package com.lavida.project.wage.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lavida.common.utils.StringUtils;
import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.project.wage.domain.NewWageAccount;
import com.lavida.project.wage.domain.ProvideRecord;
import com.lavida.project.wage.mapper.NewWageAccountMapper;
import com.lavida.project.wage.mapper.ProvideRecordMapper;
import com.lavida.project.wage.service.NewWageAccountService;

@Service
public class NewWageAccountServiceImpl implements NewWageAccountService{
	
	@Autowired
	private NewWageAccountMapper newWageAccountMapper;
	
	@Autowired
	private ProvideRecordMapper provideRecordMapper;
	
	@Override
	public Long selectNewWageAccountTotal(NewWageAccount newWageAccount) {
		// TODO Auto-generated method stub
		return newWageAccountMapper.selectNewWageAccountTotal(newWageAccount);
	}
	@Override
	public List<NewWageAccount> selectNewWageAccountList(NewWageAccount newWageAccount) {
		// TODO Auto-generated method stub
		return newWageAccountMapper.selectNewWageAccountList(newWageAccount);
	}
	@Override
	public List<NewWageAccount> selectEmpInfoNewWageAccountList(NewWageAccount newWageAccount) {
		// TODO Auto-generated method stub
		return newWageAccountMapper.selectEmpInfoNewWageAccountList(newWageAccount);
	}
	@Override
	@Transactional
	public Integer addNewWageAccountList(List<NewWageAccount> newWageAccountList) {
		// TODO Auto-generated method stub
		int result = 0;
		//判断发放记录是否存在，不存在先添加
		ProvideRecord provideRecord = new ProvideRecord();
		provideRecord.setYear(newWageAccountList.get(0).getYear());
		provideRecord.setMonth(newWageAccountList.get(0).getMonth());
		ProvideRecord provideRecord2 = provideRecordMapper.getProvideRecord(provideRecord);
		if(provideRecord2!=null) {
			provideRecord2.setCreateBy(ShiroUtils.getSysUser().getLoginName());
	    	provideRecord2.setCreateTime(new Date());
	    	provideRecordMapper.editProvideRecord(provideRecord2);
		}else {
			provideRecord2 = new ProvideRecord();
			provideRecord2.setYear(provideRecord.getYear());
			provideRecord2.setMonth(provideRecord.getMonth());
			provideRecord2.setCreateBy(ShiroUtils.getSysUser().getLoginName());
	    	provideRecord2.setCreateTime(new Date());
	    	provideRecordMapper.addProvideRecord(provideRecord2);
		}
		
		
		if(newWageAccountList.size()>0) {
			newWageAccountMapper.deleteNewWageAccount(newWageAccountList.get(0));
		}
		for(NewWageAccount newWageAccount:newWageAccountList) {
			if(newWageAccount.getJobNum()==null) {
				newWageAccount.setJobNum(newWageAccount.getEmp().getJob_id());
			}
			if(StringUtils.isEmpty(newWageAccount.getName())) {
				newWageAccount.setName(newWageAccount.getEmp().getName());
			}
			newWageAccount.setCreateBy(ShiroUtils.getSysUser().getLoginName());
			newWageAccount.setCreateTime(new Date());
			result = newWageAccountMapper.addNewWageAccount(newWageAccount);
		}
		return result;
	}
	@Override
	public Integer deleteNewWageAccount(NewWageAccount newWageAccount) {
		// TODO Auto-generated method stub
		return newWageAccountMapper.deleteNewWageAccount(newWageAccount);
	}

}
